< previous page page_677 next page >

Page 677
Events and Procedures
In standard Visual Basic, you respond to events by placing code in event procedures. You can also use event procedures in VBScript, although writing them is not as easy because the event procedure declarations are not provided for you.
As an example, consider the following HTML code, which creates two form elements, an input box and a button:
<INPUT Type="text" Name="txtLastName" Value="Smith" Size=20>
<INPUT Type="Button" Name="cmdCalculate" Value="Perform Calculation">
Because the preceding two form elements are named, all you have to do to create an event procedure is write a VBScript subroutine with the appropriate name and parameters. For example, by creating a VBScript subroutine called cmdCalculate_OnClick(), you can get IE to execute code in response to the button's Click event. The code in Listing 30.2 uses VBScript events in a simple interest calculator.
Listing 30.2 SIMPLEVBS.ZIPUsing VBScript to Access Web Page Elements
<HTML>
<HEAD><TITLE>Simple example of VBScript</TITLE></HEAD>

<SCRIPT Language = "VBScript">
<!--

Function CalcInterest(P,R,T)
    CalcInterest = P * R * T
End Function

Sub cmdCalculate_OnClick()
    Dim lPrincipal
    Dim dblRate
    Dim nYears
    Dim cInterest

    lPrincipal = CLng(txtPrincipal.value)
    dblRate = CDbl(txtRate.value)
    nYears = CInt(txtTime.value)

    cInterest = CalcInterest(lPrincipal, dblRate, nYears)

    MsgBox "Your Interest is " & FormatCurrency(cInterest)
End Sub
Sub txtRate_OnChange()
    If CDbl(txtRate.Value) > 1 Then txtRate.Value = txtRate.Value / 100
End Sub

-->
</SCRIPT>
<BODY>
<BR>

Enter Principal: <INPUT Type="Text" Name="txtPrincipal" Value="100000"><BR>
Enter Int. Rate: <INPUT Type="Text" Name="txtRate" Value="0.08"><BR>
continues

 
< previous page page_677 next page >